#include "stdafx.h"
#include "WindowRenderer.h"
#include "Element.h"

using namespace Rendering;

WindowRenderer::WindowRenderer(void)
{
}


WindowRenderer::~WindowRenderer(void)
{
}

void WindowRenderer::Initialize (HWND windowHandle)
{
	__super::Initialize();
	SetWindow(windowHandle);
}

void WindowRenderer::SetWindow(HWND windowHandle)
{
	m_windowHandle = windowHandle;
	if (m_windowHandle != NULL && (m_renderTargetSize.width == 0 || m_renderTargetSize.height == 0))
	{
		RECT rc;
		GetClientRect(m_windowHandle, &rc);
		UpdateRenderTargetSize(D2D1::SizeF(static_cast<float>(rc.right - rc.left), static_cast<float>(rc.bottom - rc.top)));
	}
	else
		CreateRenderTargetResources();
}

/// <summary>Creates the swap chain.</summary>
/// <param name="dxgiFactory">DirectX graphics interface factory.</param>
/// <param name="swapChainDesc">The swap chain description.</param>
void WindowRenderer::CreateSwapChain(IDXGIFactory2* dxgiFactory, DXGI_SWAP_CHAIN_DESC1& swapChainDesc)
{
	// Create the swap chain and then associate it with the SwapChainBackgroundPanel.
    IF_FAILED_THROW_HR(
        dxgiFactory->CreateSwapChainForHwnd(
            m_device,
			m_windowHandle,
            &swapChainDesc,
			nullptr,
            nullptr,    // allow on all displays
            &m_swapChain
            )
        );

    // Set the swap chain for the window
}

/// <summary>Renders the elements.</summary>
void WindowRenderer::Render()
{
    m_context->OMSetRenderTargets(1, &m_renderTargetView.GetInterfacePtr(), m_depthStencilView);
    Clear();

	for(auto e : m_elements)
	{
		e->Render(this);
	}
    Present();
}